import { ref, computed } from "vue";
import { defineStore } from "pinia";

import { useHomeStore } from "./home";

interface MenuItemType {
  title: string;
  index: string;
  icon?: string;
  children?: MenuItemType[];
}

export const useBannerStore = defineStore("banner", () => {
  const homeStore = useHomeStore();
  // console.log(homeStore.checkedkeys);

  const menuList = ref<MenuItemType[]>([
    {
      title: "系统首页",
      index: "/",
      icon: "position",
    },
    {
      title: "轮播图管理",
      index: "/banner",
      icon: "position",
      children: [
        {
          title: "轮播图列表",
          index: "/banner/list",
        },
        {
          title: "添加轮播图",
          index: "/banner/add",
        },
      ],
    },
    {
      title: "产品管理",
      index: "/pro",
      icon: "position",
      children: [
        {
          title: "产品列表",
          index: "/pro/list",
        },
      ],
    },
    {
      title: "账号管理",
      index: "/user",
      icon: "position",
      children: [
        {
          title: "管理员",
          index: "/user/admin",
        },
      ],
    },
    {
      title: "数据可视化",
      index: "/data",
      icon: "position",
      children: [
        {
          title: "Echarts",
          index: "/data/echarts",
        },
      ],
    },
    {
      title: "编辑器",
      index: "/editor",
      icon: "position",
      children: [
        {
          title: "富文本编辑器",
          index: "/editor/index",
        },
        {
          title: "md编辑器",
          index: "/editor/md",
        },
      ],
    },
    {
      title: "文件处理",
      index: "/excel",
      icon: "position",
      children: [
        {
          title: "导出excel",
          index: "/excel/export",
        },
        {
          title: "导入excel",
          index: "/excel/import",
        },
      ],
    },
    {
      title: "地图",
      index: "/map",
      icon: "position",
      children: [
        {
          title: "高德地图",
          index: "/map/index",
        },
      ],
    },
  ]);

  const menuList2 = computed(() => {
    if (localStorage.getItem("adminname") === "admin") {
      return menuList.value;
    } else {
      const keys = JSON.parse(homeStore.checkedkeys as string);
      const arr = menuList.value.filter((item) => keys.includes(item.index));

      return arr;
    }
  });

  return { menuList, menuList2 };
});
